from flask import Flask, request, session, redirect, url_for

app = Flask(__name__)

app.secret_key = 'lyk03'


@app.route('/index')
def index():
    if 'username' in session:
        return f'你好: {session.get('username')}'
    return redirect(url_for('login'))


@app.route('/login', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        session['username'] = request.form['username']
        return redirect(url_for('index'))
    return '''
        <form method="post">
            <label>
                <input type="text" name=username>
            </label>
            <p>
                <input type="submit" value="登录">
            </p>
        </form>
    '''


if __name__ == '__main__':
    app.run()
